//
//  PieChartView.m
//  TotalDemo
//
//  Created by gujian on 16/3/3.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "PieChartView.h"

@interface PieChartView()

@property (nonatomic, assign) CGFloat progress;

@end

@implementation PieChartView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
    }
    
    return self;
    
}

//- (void)drawRect:(CGRect)rect
//{
//    [super drawRect:rect];
//    
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    
//    //get the square rect
//    CGFloat width = MIN(rect.size.width, rect.size.height);
//    CGFloat originX = (rect.size.width - width)/2.0 + rect.origin.x;
//    CGFloat originY = (rect.size.height - width)/2.0 + rect.origin.y;
//    CGRect squareRect = CGRectMake(originX, originY, width, width);
//    
//    //draw background pie
//    CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
//    CGContextMoveToPoint(context, originX+width/2.0, originY+width/2.0);
//    CGContextAddEllipseInRect(context, squareRect);
//    CGContextClosePath(context);
//    CGContextDrawPath(context, kCGPathFill);
//    
//    
//    //draw foreground pie according to the ratio
////    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
////    CGContextMoveToPoint(context, originX+width/2.0, originY+width/2.0);
////    CGContextAddArc(context, originX+width/2.0, originY+width/2.0, width/2.0,0, _progress * 2 * M_PI, false);
////    CGContextClosePath(context);
////    CGContextDrawPath(context, kCGPathFill);
//
//    [[UIColor redColor] set];
//
//    
//    
//
//    
//}

- (CAShapeLayer *)sharpLayer
{
    CAShapeLayer *sharpLayer = [CAShapeLayer layer];
    sharpLayer.fillColor = [UIColor redColor].CGColor;
//    sharpLayer.strokeColor = [UIColor redColor].CGColor;
    sharpLayer.lineWidth = 1.0;
    sharpLayer.path = [self fill].CGPath;

    CABasicAnimation *animation = [self fillAnimation];
    
    [sharpLayer addAnimation:animation forKey:@"startAngle"];
    [sharpLayer addAnimation:[self animation] forKey:@"endAngle"];
    
    return sharpLayer;
    
    
}


- (CGMutablePathRef)animationPath
{
    CGRect rect = self.frame;
    
    CGFloat width = MIN(rect.size.width, rect.size.height);
    CGFloat originX = (rect.size.width - width)/2.0 + rect.origin.x;
    CGFloat originY = (rect.size.height - width)/2.0 + rect.origin.y;
    
    CGMutablePathRef mutablePathRef = CGPathCreateMutable();
    CGPathAddArc(mutablePathRef, NULL, originX+width/2.0, originY+width/2.0, width/2.0, 0, _progress * 2 * M_PI, false);
    //添加endPoint到圆心的线条  圆心到beginPoint的线条
//    CGPathAddLineToPoint(mutablePathRef, NULL, originX+width/2.0, originY+width/2.0);
//    CGPathAddLineToPoint(mutablePathRef, NULL, originX+width, originY+width/2.0);
//    CGPathCloseSubpath(mutablePathRef);
    //    CGContextAddPath(context, mutablePathRef);
    //    CGContextDrawPath(context, kCGPathFill);
    
//    CFRelease(mutablePathRef);
    
    return mutablePathRef;
}

- (UIBezierPath *)fill{
    //需要多少度的圆弧
    CGRect rect = self.frame;
    
    CGFloat width = MIN(rect.size.width, rect.size.height);
    CGFloat originX = (rect.size.width - width)/2.0 + rect.origin.x;
    CGFloat originY = (rect.size.height - width)/2.0 + rect.origin.y;
    
    UIBezierPath * bezier = [UIBezierPath bezierPathWithArcCenter:CGPointMake(originX+width/2.0, originY+width/2.0) radius:width/2.0 startAngle:0 endAngle:_progress * 2 * M_PI clockwise:YES];

    
    return bezier;
}

- (CABasicAnimation *)fillAnimation
{
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"startAngle"];
    basicAnimation.duration = 5.0;
    basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    basicAnimation.fillMode = kCAFillModeForwards;
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.fromValue = @(0.f);
    basicAnimation.toValue = @( _progress * 2 * M_PI);
    
    return basicAnimation;
}

- (CABasicAnimation *)animation{
    CABasicAnimation * fillAnimation = [CABasicAnimation animationWithKeyPath:@"endAngle"];//strokeEnd
    fillAnimation.duration = 5.0;
    fillAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    fillAnimation.fillMode = kCAFillModeForwards;
    fillAnimation.removedOnCompletion = NO;
    fillAnimation.fromValue = @(_progress * 2 * M_PI);
    fillAnimation.toValue = @(0.f);
    
    return fillAnimation;
}


#pragma mark - public methods

- (void)setProgress:(CGFloat)progress
{
    if (_progress == progress) {
        return;
    }
 

    for (CALayer *layer in self.layer.sublayers) {
        [layer removeAllAnimations];
        [layer removeFromSuperlayer];
    }
    
    if (progress > 1.0f) {
        progress = 1.0f;
    }
    
    if (progress < 0.0f) {
        progress = 0.0f;
    }
    
    _progress = progress;
    
//    [self setNeedsDisplay];
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];

    CAShapeLayer *sharpeLayer = [self sharpLayer];
    [CATransaction commit];
    [self.layer addSublayer:sharpeLayer];
//    [self.layer addSublayer:<#(nonnull CALayer *)#>];
}








@end
